Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

344
Views
Why use a generic type and then limit it to one type with "where"?

What's the advantage of using this:

DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow

where you can just do this:

DataTable CopyToDataTable<T>(this DataRow[] source)

Excuse me if this is a dumb question, I'm new at this

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think there are two parts to this.

The first is that accepting an IEnumerable is more flexible than accepting an array. What if the user has a List<DataRow>, or some other collection type? What if they want to pass the result of a linq query? Accepting an array forces them to allocate a new array just to pass to CopyToDataTable, and that's unnecessary cost.

There's no reason why CopyToDataTable needs an array: it just needs an IEnumerable. So it's best if it just accepts an IEnumerable.

That said, due to covariance it would be possible to use the signature:

DataTable CopyToDataTable(this IEnumerable<DataRow> source)

... and users would be able to pass e.g. an IEnumerable<TableDetailsRow> (where TableDetailsRow extends DataRow). However, covariance was only introduced in C# 4, and that method has been around since .NET 3.5, which means it was probably written in C# 3. Since covariance wasn't available then, generics was the next best thing.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!